---
title: "Reference: Workflow.map() | Workflows"
description: Documentation for the `Workflow.map()` method in workflows, which maps output data from a previous step to the input of a subsequent step.
---

# Workflow.map()

The `.map()` method maps output data from a previous step to the input of a subsequent step, allowing you to transform data between steps.

## Usage example

```typescript copy
workflow.map(async ({ inputData }) => `${inputData.value} - map`
```

## Parameters

<PropertiesTable
  content={[
    {
      name: "mappingFunction",
      type: "(params: { inputData: any }) => any",
      description:
        "Function that transforms input data and returns the mapped result",
      isOptional: false,
    },
  ]}
/>

## Returns

<PropertiesTable
  content={[
    {
      name: "workflow",
      type: "Workflow",
      description: "The workflow instance for method chaining",
    },
  ]}
/>

## Using `inputData`

Use `inputData` to access the full output of the previous step.

```typescript {3} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
  .then(step1)
  .map(({ inputData }) => {
    console.log(inputData);
  })
```

## Using `getStepResult()`

Use `getStepResult()` to access the full output of a specific step by referencing the step's instance.

```typescript {3} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
  .then(step1)
  .map(async ({ getStepResult }) => {
    console.log(getStepResult(step1));
  })
```

## Using `getInitData()`

Use `getInitData()` to access the initial input data provided to the workflow.

```typescript {3} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
  .then(step1)
  .map(async ({ getInitData }) => {
      console.log(getInitData());
  })
```

## Using `mapVariable()`

The object form of `.map()` provides an alternative declarative syntax for mapping fields. Instead of writing a function, you define an object where each key is a new field name and each value uses `mapVariable()` to extract data from previous steps or workflow input.
Import `mapVariable()` from the workflows module:

```typescript filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
import { mapVariable } from "@mastra/core/workflows";
```

### Extracting fields from step outputs

Use `mapVariable()` with `step` to extract a specific field from a step's output and map it to a new field name. The `path` parameter specifies which field to extract.
In this example, the `value` field from `step1`'s output is extracted and mapped to a new field called `details`:

```typescript {3-6} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
  .then(step1)
  .map({
    details: mapVariable({
      step: step1,
      path: "value"
    })
  })
```

### Extracting fields from workflow input

Use `mapVariable()` with `initData` to extract a specific field from the workflow's initial input data. This is useful when you need to pass the original workflow input to a later step.
In this example, the `value` field from the workflow's input is extracted and mapped to a field called `details`:

```typescript {6-9} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
export const testWorkflow = createWorkflow({...});

testWorkflow
  .then(step1)
  .map({
    details: mapVariable({
      initData: testWorkflow,
      path: "value"
    })
  })
```

## Related

- [Input Data Mapping](/docs/v1/workflows/control-flow#input-data-mapping)
